package wjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wjh.mapper.AdminMapper;
import wjh.pojo.Admin;
import wjh.pojo.Num;
import wjh.pojo.Plan;
import wjh.pojo.Sex;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/9/2 9:08
 */
@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(Admin admin) {
        return adminMapper.login(admin);
    }

    @Override
    public List<Num> echarts() {
        return adminMapper.echarts();
    }

    @Override
    public void addPaiBan(Plan plan) {
        adminMapper.addPaiBan(plan);
    }

    @Override
    public void updatePaiBan(Plan plan) {
        adminMapper.updatePaiBan(plan);
    }

    @Override
    public List<Sex> echarts2() {
        return adminMapper.echarts2();
    }

    @Override
    public Map<String, Integer> findAgeGroupCounts() {
        List<Integer> ages = adminMapper.findAllAges();
        Map<String, Integer> ageGroupCounts = new HashMap();
        for (int i = 0; i <= 9; i++) {
            String key = (i * 10) + "-" + ((i + 1) * 10 - 1);
            if (i == 9) {
                key = "90-100"; // 特殊处理90-100岁
            }
            ageGroupCounts.put(key, 0);
        }

        for (Integer age : ages) {
            if (age >= 0 && age <= 100) {
                int ageGroup = age / 10;
                String key = (ageGroup * 10) + "-" + ((ageGroup + 1) * 10 - 1);
                if (ageGroup == 9) {
                    key = "90-100"; // 特殊处理90-100岁
                }
                ageGroupCounts.put(key, ageGroupCounts.get(key) + 1);
            }
        }
        return  ageGroupCounts;
    }


    @Override
    public List<Integer> findAllAges() {
        return adminMapper.findAllAges();
    }

}
